package com.hua.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.common.CustomError;
import com.hua.entities.Category;
import com.hua.entities.Dish;
import com.hua.entities.Setmeal;
import com.hua.mapper.CategoryMapper;
import com.hua.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

@Service
public class CategoryServiceImpl extends ServiceImpl <CategoryMapper, Category> implements CategoryService {

    @Autowired
    DishServiceImpl dishService;
    @Autowired
    SetmealServiceImpl setmealService;

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        LambdaQueryWrapper<Dish> q = new LambdaQueryWrapper<>();
        q.eq(Dish::getCategoryId,id);
        if(dishService.count(q)>0){
            //抛出一个自定义异常
            throw new CustomError("该分类已经关联菜品~~~,无法删除");

        }


        LambdaQueryWrapper<Setmeal>s = new LambdaQueryWrapper<>();
        s.eq(Setmeal::getCategoryId,id);
        if(setmealService.count(s)>0){
            throw new CustomError("该分类已经关联套餐~~~,无法删除");
        }

        //处理删除
        return super.removeById(id);
    }
}
